package com.yuncheng.spcyApi.controller;

import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.partyService.PartyXxzlService;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.entity.PartyRsXxzl;
import com.yuncheng.spcyApi.entity.SpcySqsxWjjl;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "党建-学习资料")
@RestController
@RequestMapping("/api/spzx-spcy/partyXxzl")
public class PartyXxzlController {

    @Resource
    private PartyXxzlService partyXxzlService;

    @Log(title = "学习动态-分页查询")
    @ApiOperation(value = "学习动态-分页查询")
    @GetMapping(value = "queryXxdtPageList")
    public HttpResult<?> queryXxdtPageList(PartyRsXxzl query, PageVo pageVo, OrderItemVo orderItemVo) {
        query.setFzllx(SpcyCommonConstant.XXZL_ZLLX_DJLLXX);
        query.setFfbzt(SpcyCommonConstant.XXZL_FBZT_YFB);
        return partyXxzlService.queryXxzlPageList(query,pageVo.getPageNo(),pageVo.getPageSize(), orderItemVo);
    }

    /************************************[ 企业-廉洁宣传 ]**************************************************************************/

    @Log(title = "廉洁宣传资料管理-分页查询")
    @ApiOperation(value = "廉洁宣传资料管理-分页查询")
    @GetMapping(value = "queryLjxczlGlPageList")
    public HttpResult<?> queryLjxczlglPageList(PartyRsXxzl query, PageVo pageVo, OrderItemVo orderItemVo) {
        if (StringUtils.isBlank(query.getFzllx())) {
            query.setFzllx("0");
        }
        if (query.getFzllx().equals(SpcyCommonConstant.XXZL_ZLLX_CXDRHD)){
            query.setFssdzb(SpcyCommonConstant.XXZL_SSDZB);
        }
        return partyXxzlService.queryXxzlPageList(query,pageVo.getPageNo(),pageVo.getPageSize(), orderItemVo);
    }

    @Log(title = "廉洁宣传资料学习-分页查询")
    @ApiOperation(value = "廉洁宣传资料学习-分页查询")
    @GetMapping(value = "queryLjxczlXxPageList")
    public HttpResult<?> queryLjxczlxxPageList(PartyRsXxzl query,PageVo pageVo,OrderItemVo orderItemVo) {
        if (StringUtils.isBlank(query.getFzllx())) {
            query.setFzllx("0");
        }
        query.setFfbzt(SpcyCommonConstant.XXZL_FBZT_YFB);

        if (query.getFzllx().equals(SpcyCommonConstant.XXZL_ZLLX_CXDRHD)){
            query.setFfbzt("");
            query.setFssdzb(SpcyCommonConstant.XXZL_SSDZB);
        }
        return partyXxzlService.queryXxzlPageList(query,pageVo.getPageNo(),pageVo.getPageSize(),orderItemVo);
    }

    @Log(title = "廉洁宣传资料-添加/修改")
    @ApiOperation(value = "廉洁宣传资料-添加/修改")
    @PostMapping(value = "saveLjxczl")
    public HttpResult saveLjxczl(@RequestBody PartyRsXxzl bean) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (StringUtils.isNotBlank(bean.getId())){
            bean = partyXxzlService.updateXxzl((HashMap<String, String>) BeanUtils.describe(bean));
        }else {
            if (StringUtils.isBlank(bean.getFzllx())) {
                throw new BusinessException("操作失败，数据资料类型参数丢失！");
            }
            bean = partyXxzlService.addXxzl(bean);
        }

        return HttpResultVo.HttpResultSave(bean);
    }



    @Log(title = "学习资料-删除")
    @ApiOperation(value = "学习资料-删除")
    @DeleteMapping(value = "deleteXxzl")
    public HttpResult deleteXxzl(@RequestParam(value = "id",defaultValue = "")String id){
        partyXxzlService.deleteXxzl(id);

        return HttpResult.ok("删除成功");
    }

    @Log(title = "学习资料-发布")
    @ApiOperation(value = "学习资料-发布")
    @GetMapping(value = "releaseXXzl")
    public HttpResult releaseXXzl(@RequestParam(value = "id",defaultValue = "")String id){
        partyXxzlService.releaseXXzl(id);

        return HttpResult.ok("发布成功");
    }

    @Log(title = "学习资料-下架")
    @ApiOperation(value = "学习资料-下架")
    @GetMapping(value = "xjXxzl")
    public HttpResult xjXxzl(@RequestParam(value = "id",defaultValue = "")String id){
        partyXxzlService.xjXxzl(id);

        return HttpResult.ok("下架成功");
    }

    @Log(title = "获取学习资料详情")
    @ApiOperation(value = "获取学习资料详情")
    @ApiImplicitParam(name = "id",value = "当前数据id")
    @GetMapping(value = "queryXxzlXq")
    public HttpResult queryXxzlXq(@RequestParam(value = "id",defaultValue = "")String id){
        Map map = partyXxzlService.queryXxzlXq(id);
        return HttpResult.ok(map);
    }

    @Log(title = "记录阅读次数")
    @ApiOperation(value = "记录阅读次数")
    @GetMapping(value = "recordReadCount")
    public HttpResult recordReadCount(@RequestParam(value = "id",defaultValue = "")String id){
        partyXxzlService.updateXxzlYdcs(null, id);
        return HttpResult.ok("操作成功");
    }

    @Log(title = "党建学习-附件分类-ppt/视频/pdf/mp3")
    @ApiOperation(value = "党建学习-附件分类-ppt/视频/pdf/mp3")
    @ApiImplicitParam(name = "id",value = "当前数据id")
    @GetMapping(value = "queryDjxxTypeFjList")
    public HttpResult queryDjxxTypeFjList(@RequestParam(value = "id",defaultValue = "")String id){
        Map map = partyXxzlService.queryDjxxTypeFjList(id);
        return HttpResult.ok(map);
    }

    @Log(title = "学习资料-附件列表查询")
    @ApiOperation(value = "学习资料-附件列表查询")
    @ApiImplicitParam(name = "id",value = "当前数据id")
    @GetMapping(value = "queryXxzlFjList")
    public HttpResult queryXxzlFjList(@RequestParam(value = "id",defaultValue = "")String id){
        List<SpcySqsxWjjl> wjjlList = partyXxzlService.queryXxzlFileRecords(id);
        return HttpResultVo.HttpResultList(wjjlList);
    }


    @Log(title = "附件-上传")
    @ApiOperation(value = "附件-上传")
    @ApiImplicitParam(name = "id",value = "当前数据id")
    @PostMapping(value = "/uploadFile")
    public HttpResult<?> uploadFile(@RequestPart("file") MultipartFile[] uploadFile,
                                    @RequestParam(value = "id",defaultValue = "") String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("上传失败，数据参数为空！");
        }

        PartyRsXxzl xxzl = partyXxzlService.queryXxzlById(id);
        if (xxzl == null){
            throw new BusinessException("没有想匹配的数据！");
        }
        String type = xxzl.getFzllx();
        partyXxzlService.uploadFile(uploadFile,id,type);

        return HttpResult.ok("上传成功");
    }

    @Log(title = "附件-删除")
    @ApiOperation(value = "附件-删除")
    @ApiImplicitParam(name = "id",value = "附件id")
    @DeleteMapping(value = "deleteFile")
    public HttpResult deleteFile(@RequestParam(value = "id",defaultValue = "")String id){
        partyXxzlService.deleteFile(id);

        return HttpResult.ok("删除成功");
    }

    @Log(title = "附件-下载")
    @ApiOperation(value = "附件-下载")
    @ApiImplicitParam(name = "id",value = "附件id")
    @GetMapping(value = "downFile")
    public void downFile(@RequestParam(value = "id",defaultValue = "")String id, HttpServletResponse response){
        partyXxzlService.downFile(id, response);
    }

    /************************************[ 企业-创新党日活动 ]**************************************************************************/

    @Log(title = "主题党日活动-分页查询")
    @ApiOperation(value = "主题党日活动-分页查询")
    @GetMapping(value = "queryZtdrhdPageList")
    public HttpResult<?> queryZtdrhdPageList(PartyRsXxzl query,PageVo pageVo,OrderItemVo orderItemVo) {
        query.setFzllx(SpcyCommonConstant.XXZL_ZLLX_CXDRHD);
        query.setFssdzb(SpcyCommonConstant.XXZL_SSDZB);
        return partyXxzlService.queryXxzlPageList(query,pageVo.getPageNo(),pageVo.getPageSize(),orderItemVo);
    }

    @Log(title = "主题党日活动学习-分页查询")
    @ApiOperation(value = "主题党日活动学习-分页查询")
    @GetMapping(value = "queryZtdrhdxxPageList")
    public HttpResult<?> queryZtdrhdxxPageList(PartyRsXxzl query,PageVo pageVo, OrderItemVo orderItemVo) {
        query.setFzllx(SpcyCommonConstant.XXZL_ZLLX_CXDRHD);
        query.setFssdzb(SpcyCommonConstant.XXZL_SSDZB);
        return partyXxzlService.queryXxzlPageList(query,pageVo.getPageNo(),pageVo.getPageSize(), orderItemVo);
    }

    @Log(title = "其他党日活动-分页查询")
    @ApiOperation(value = "其他党日活动-分页查询")
    @GetMapping(value = "queryCxdrhdGlPageList")
    public HttpResult<?> queryCxdrhdGlPageList(PartyRsXxzl query,PageVo pageVo, OrderItemVo orderItemVo) {
        query.setFzllx(SpcyCommonConstant.XXZL_ZLLX_CXDRHD);
        return partyXxzlService.queryXxzlPageList(query,pageVo.getPageNo(),pageVo.getPageSize(), orderItemVo);
    }

    @Log(title = "其他党日活动学习-分页查询")
    @ApiOperation(value = "其他党日活动学习-分页查询")
    @GetMapping(value = "queryCxdrhdXxPageList")
    public HttpResult<?> queryCxdrhdXxPageList(PartyRsXxzl query,PageVo pageVo, OrderItemVo orderItemVo) {
        query.setFzllx(SpcyCommonConstant.XXZL_ZLLX_CXDRHD);
        query.setFwczt(SpcyCommonConstant.XXZL_DRHD_WCZT_YWC);
        return partyXxzlService.queryXxzlPageList(query,pageVo.getPageNo(),pageVo.getPageSize(), orderItemVo);
    }

    @Log(title = "其他党日活动-添加/修改")
    @ApiOperation(value = "其他党日活动-添加/修改")
    @PostMapping(value = "saveCxdrhd")
    public HttpResult saveCxdrhd(@RequestBody PartyRsXxzl bean) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {

        bean.setFzllx(SpcyCommonConstant.XXZL_ZLLX_CXDRHD);
        if (StringUtils.isNotBlank(bean.getId())){
            bean = partyXxzlService.updateXxzl((HashMap<String, String>) BeanUtils.describe(bean));
        }else {
            bean = partyXxzlService.addXxzl(bean);
        }

        return HttpResultVo.HttpResultSave(bean);
    }

    @Log(title = "党日活动-提交")
    @ApiOperation(value = "党日活动-提交")
    @PostMapping(value = "drhdSubmit")
    public HttpResult drhdSubmit(@RequestBody PartyRsXxzl xxzl) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        xxzl.setFssdzb(SpcyCommonConstant.XXZL_SSDZB);
        xxzl.setFzllx(SpcyCommonConstant.XXZL_ZLLX_CXDRHD);
        partyXxzlService.drhdSubmit(xxzl, (HashMap<String, String>) BeanUtils.describe(xxzl));

        return HttpResult.ok("提交成功");
    }

    @Log(title = "其他支部党日活动-提交")
    @ApiOperation(value = "其他支部党日活动-提交")
    @PostMapping(value = "qtdrhdSubmit")
    public HttpResult qtdrhdSubmit(@RequestBody PartyRsXxzl xxzl) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        xxzl.setFzllx(SpcyCommonConstant.XXZL_ZLLX_CXDRHD);
        partyXxzlService.drhdSubmit(xxzl, (HashMap<String, String>) BeanUtils.describe(xxzl));

        return HttpResult.ok("提交成功");
    }

}
